'use strict'
function updateProductById(id) {
    return new Promise((res, rej) => {
        $.ajax({
            url: `${url}/product/${id}`,
            type: 'PUT',
            success: response => {
                res(response)
            }
        });
    })
}

function delProductById(id) {
    return new Promise((res, rej) => {
        $.ajax({
            url: `${url}/product/${id}`,
            type: 'DELETE',
            success: response => {
                res(response);
            }
        });
    })
}

function get(url) {
    return new Promise((res, rej) => {
        $.get(url, data => {
            res(data);
        });
    })
}

/**
 * 
 * @param {array} data 数组套对象
 * @param {jquery元素} table 被插入的数据表
 */
function render(data,table) {
    for (const item of data) {
        table.append(`<tr class="dataRow" key="${item.id}"></tr>`);
        const row = $('.dataRow').last();

        for (const key in item) {
            if (Object.hasOwnProperty.call(item, key)) {
                const element = item[key];
                row.append(`<td class="${key}">${element}</td>`);
            }
        }

        row.append(`
            <input type="button" class="edit" value="编辑" onclick=update(${item.id})>
            <input type="button" class="remove" value="删除" onclick=del(${item.id})>
        `);
    }
}